#include "Scene0910.h"

Scene * Scene0910::createScene()
{
	return Scene0910::create();
}

bool Scene0910::init()
{
	if (!Scene::init()) {
		return false;
	}
	visibleSize = Director::getInstance()->getVisibleSize();
	Sprite *bg = Sprite::create("20180910/bg.png");
	tablebg = Sprite::create("20180910/tablebg.png");
	Sprite *match = Sprite::create("20180910/match.png");
	Sprite *normal = Sprite::create("20180910/normal.png");
	Sprite *close = Sprite::create("20180910/close.png");
	Sprite *bg2 = Sprite::create("20180910/bg2.jpg");
	bg->setPosition(visibleSize / 2);
	bg2->setPosition(visibleSize / 2);
	close->setPosition(visibleSize.width / 2 + bg->getContentSize().width*0.96 / 2, visibleSize.height / 2 + bg->getContentSize().height*0.96 / 2);
	match->setPosition(visibleSize.width / 2 + 180, visibleSize.height / 2 + 150);
	normal->setPosition(visibleSize.width / 2 - 180, visibleSize.height / 2 + 150);
	tablebg->setPosition(visibleSize.width / 2, visibleSize.height / 2 - 50);
	addChild(bg2);
	addChild(bg);
	addChild(close);
	addChild(match);
	addChild(normal);
	addChild(tablebg);

	menuArray = __Array::createWithCapacity(10);
	for (int i = 0; i < 10; i++)
	{
		menuArray->addObject(__String::createWithFormat("cell%d", i));
	}
	menuArray->retain();//增加自动计数 防止改数据被释放
						//menuArray->release();//减少自动计数
	TableView *tableView = TableView::create(this, tablebg->getContentSize());
	tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);//从上往下排序
	tableView->setPosition(tablebg->getPositionX() - tablebg->getContentSize().width / 2, tablebg->getPositionY() - tablebg->getContentSize().height / 2);
	CCLOG("%f", tablebg->getPositionY());
	tableView->setDelegate(this);
	addChild(tableView);
	return true;
}

void Scene0910::tableCellTouched(TableView * table, TableViewCell * cell)//表格某一项被点击
{
	CCLOG("%d", cell->getIdx());

	Scene0917 *scene = (Scene0917 *)Scene0917::createScene();
	scene->setRoomId((int)cell->getIdx() + 1);
	Director::getInstance()->replaceScene(scene);
}

Size Scene0910::tableCellSizeForIndex(TableView * table, ssize_t idx)//表格某一项的尺寸
{
	return Size(tableCellWidth, tableCellHeight);
}

TableViewCell * Scene0910::tableCellAtIndex(TableView * table, ssize_t idx)//表格某一项的UI布局
{
	TableViewCell *cell = table->dequeueCell();//看看表格里有没有可以复用的cell
	if (!cell) {//没有的话
				//创建一个新的
		cell = TableViewCell::create();
	}
	cell->removeAllChildrenWithCleanup(true);//清除表格里所有的内容，防止cell上的内容重叠
	__String *content = (__String *)menuArray->getObjectAtIndex(idx);
	Label *label = Label::createWithSystemFont(StringUtils::format("%s", content->getCString()), "Arial", 40);
	label->setPosition(tablebg->getContentSize().width / 2, tableCellHeight / 2);
	Sprite *ccell = Sprite::create("20180910/cell.png");
	ccell->setPosition(tablebg->getContentSize().width / 2, tableCellHeight / 2);
	cell->addChild(ccell);
	cell->addChild(label);
	return cell;
}

ssize_t Scene0910::numberOfCellsInTableView(TableView * table)//表格有多少项
{
	//count:获取元素个数
	return menuArray->count();
}

